package gui.core;

import gui.core.model.StudentListCellRenderer;
import gui.core.model.StudentListModel;

import java.awt.BorderLayout;
import java.awt.LayoutManager;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import model.ServiceFactory;
import model.StudentService;

public class Browser extends JPanel
{
	private JTabbedPane _tabbedPane; // pane ce imati 2 taba - za Students i za Lectures
	private JList _studentList;
	private JList _lessonsList;
	private JTextField _searchText;
	private ServiceFactory _serviceFactory;
	
	private StudentListModel _studentListModel;
	
	public Browser( ServiceFactory sf )
	{
		_serviceFactory = sf;
		_tabbedPane = new JTabbedPane();
		_studentList = new JList<>();
		
		_lessonsList = new JList<>();
		_searchText = new JTextField(20);
		
		StudentService ss = null;
		
		try
		{
			ss = _serviceFactory.getService(StudentService.class);
		}
		catch(ClassNotFoundException e){}
		
		_studentListModel = new StudentListModel(ss);
		
		ss.addStudentServiceObserver(_studentListModel);
		ss.requestAllStudents();
	}
	
	public void initGuiComponents()
	{
		JPanel studentPanel = new JPanel();
		studentPanel.add(_studentList);
		
		JPanel lessonsPanel = new JPanel();
		lessonsPanel.add(_lessonsList);
		
		_tabbedPane.addTab("Students", studentPanel);
		_tabbedPane.addTab("Lessons", lessonsPanel);
		
		this.setLayout(new BorderLayout());
		
		this.add(_searchText, BorderLayout.NORTH);
		this.add(_tabbedPane, BorderLayout.CENTER);
		
		_studentList.setModel(_studentListModel);
		_studentList.setCellRenderer(new StudentListCellRenderer());
	}
}
